package day05;

import java.util.Arrays;
import java.util.Random;

public class HomeWork {
    public static void main(String[] args) {
        //1.数组练习：访问、遍历、复制
        int [] arr = new int[10];
        System.out.println(arr.length);  //输出arr的长度
        arr [0] = 100;
        System.out.println(arr[arr.length-1]);  //输出最后一个元素的值


        for (int i = 0; i < arr.length; i++) {
            arr [i] = (int)(Math.random()*100);
            System.out.println(arr[i]);
        }

        int a [] ={10,20,30,40,50};
        a = Arrays.copyOf(a,a.length+1);
        for (int i = 0; i < a.length; i++) {
            System.out.println(arr[i]);
        }

        //2. MaxOfArray求数组元素最大值
        int [] arr1 = new int [10];
        for (int i = 0; i < arr1.length; i++) {
            arr1 [i] = (int)(Math.random()*100);
            System.out.println(arr1[i]);
        }
        int max = arr1 [0];
        for (int i = 0; i < arr1.length; i++) {
            if(arr1 [i] > max){
                max = arr1 [i];
            }
        }
        System.out.println("最大值为："+max);

       /*
       3.方法的练习：
        要求：
        定义say()无参无返回值方法，调用测试
        定义say()有一个参无返回值方法，调用测试
        定义say()有两个参无返回值的方法，调用测试
        定义sum()两个参有返回值方法，调用测试
        定义generateArray()用于生成整型数组填充随机数并返回数组，调用测试
        */

        say();
        say("张三");
        say("张三",38);

        int b = sum(5,6);
        System.out.println(b);

        int m =5 , n = 6;
        int c = sum(m,n);
        System.out.println(c);

        int [] d = generateArray(5,100);
        System.out.println("数组的长度为："+d.length);
        for (int i = 0; i < d.length; i++) {
            System.out.println(d[i]);
        }

        int [] e = generateArray(8,20);
        System.out.println("第1个元素的值："+e[0]);
        for (int i = 0; i < e.length; i++) {
            System.out.println(e[i]);
        }

    }

    public static void say() {
        System.out.println("大家好，我叫张三，今年38岁了");
    }

    public static void say(String name){
        System.out.println("大家好，我叫"+name+",今年38岁了");
    }

    public static void say(String name ,int age){
        System.out.println("大家好，我叫"+name+",今年"+age+"岁了");
    }

    public static int sum(int num1 , int num2){
        int num = num1 +num2;
        return num;
    }

    public static int [] generateArray(int len ,int max){
        Random r = new Random();
        int [] arr = new int[len];
        for (int i = 0; i < arr.length; i++) {
            arr [i] = r.nextInt(max+1);
        }
        return arr;
    }
}
